package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.net.DatagramSocket;
import java.util.List;

// 套餐模块
@Api(tags = "套餐管理")
@Slf4j
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService;

    // 套餐列表
    @ApiOperation(value = "列表查询套餐信息")
    @Cacheable(value = "order",key = "#name")
    @GetMapping("/setmeal/page")
    public ResultInfo findByPage(
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            String name) { // 1.接收参数
        // 2.调用service查询
        Page<Setmeal> page = setmealService.findByPage(pageNum, pageSize, name);
        // 3.返回结果
        return ResultInfo.success(page);

    }

    // 根据分类查询菜品列表
    @ApiOperation(value = "根据分类查询菜品列表")
    @Cacheable(value = "order",key = "#categoryId")
    @GetMapping("/dish/list")
    public ResultInfo findListBySetmeal(Long categoryId,String name) {// 1.接收参数
        // 2.调用service
        List<Dish> dishList = dishService.findListBySetmeal(categoryId,name);
        // 3.返回结果
        return ResultInfo.success(dishList);
    }



    // 新增套餐
    @ApiOperation(value = "新增套餐")
    @CacheEvict(value = "setmeal",key ="#setmeal.categoryId")
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal) { // 1.接收参数
        // 2.调用service新增
        setmealService.save(setmeal);
        // 3.返回结果
        return ResultInfo.success();
    }


    //修改时回显数据
    @ApiOperation(value = "根据ID查询套餐信息")
    @Cacheable(value = "order",key = "#id")
    @GetMapping("/setmeal/{id}")
    public ResultInfo find(@PathVariable(value = "id") Long id){

        return ResultInfo.success(setmealService.findById(id));
    }

    //修改
    @ApiOperation(value = "修改套餐信息")
    @CacheEvict(value = "setmeal",allEntries = true)
    @PutMapping("/setmeal")
    public ResultInfo update(@RequestBody Setmeal setmeal){
        //调用service
        setmealService.updateSetmeal(setmeal);

        return ResultInfo.success();
    }

    //起售,停售
    @ApiOperation(value = "套餐的起售与停售")
    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo updateStatusSetmeal(@PathVariable(value = "status") Integer status,
                                          @RequestParam List<Long> ids){
        //调用service
        setmealService.updateStatusSetmeal(status,ids);

        return ResultInfo.success();
    }

    // 删除套餐
    @ApiOperation(value = "删除套餐")
    @CacheEvict(value = "setmeal",allEntries = true)
    @DeleteMapping("/setmeal")
    public ResultInfo deleteBatch(@RequestParam List<Long> ids) { // 1.接收参数
        // 2.调用service删除
        setmealService.deleteBatch(ids);
        // 3.返回结果
        return ResultInfo.success();
    }

}
